package com.helloandroid.vo

import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.helloandroid.R

interface  IDeepCopy<T>{
    fun deepCopy():T
}


enum class Type(val value:Int){
    Shoucitixian(1), // 首次提现
    BuShu(2), // 步数
    BangdingWX(3), // 绑定微信
    Share(4), // 分享
    TianTianHongBao(5), // 天天红包赛
    ZaoWanDaKa(6), // 早晚打卡
    ChiFan(7), // 餐补
    TianTianYunDong(8), // 天天运动
    ViewVideo(9),// 看视频
    HeShui(10), // 喝水打卡
    ZhuanPan(11),// 转盘
    ChengYu(12), // 猜词语
    HongBaoYu(13), // 红包雨
    JiFuZi(14), // 积 福字
}

data class ZhuanDataItem(
                val taskId:Int,
                val resIcon:Int,
                val title:String,
                val coin:Int,
                val desc:String,
                val type:Type,
                val refresh:Boolean = true,
                val target:Int = -1
)

class ViewHolder(private val rootView: View,
                 val ivIcon: ImageView,
                 val tvTitle: TextView,
                 val tvDesc: TextView,
                 val tvCoin: TextView,
                 val tvTargetValue: TextView,
                 val btn: Button
): RecyclerView.ViewHolder(rootView)

//---------------------------------------------
enum class BtnState{
    Geted, // 已领取
    Getable, // 可以领取
    UnCompleted, // 还未完成
    Unknow, // 未知
}

interface IStateAndCount{
    var state:Int
    var count:Int
    fun setToGeted(){
        state = 1  // 1表示已领
    }
    fun setByRes(res:TaskReportResponse){
        state = res.state
        count = res.count
    }
}

data class FirstTiXian(override var state:Int, override var count:Int):IStateAndCount
data class DailyBuShu(override var state:Int, override var count:Int):IStateAndCount
data class BindWx(override var state:Int, override var count:Int):IStateAndCount
data class DailyShare(override var state:Int, override var count:Int):IStateAndCount
data class DailyHongBaoMatch(override var state:Int, override var count:Int):IStateAndCount
data class DailySport(override var state:Int, override var count:Int):IStateAndCount

data class DailyVideo(override var state:Int, override var count:Int):IStateAndCount
data class DailyHeShui(override var state:Int, override var count:Int):IStateAndCount
data class DailyZaoWan(override var state:Int, override var count: Int):IStateAndCount
data class DailyChiFan(override var state: Int, override var count:Int):IStateAndCount
data class DailyZhuanPan(override var state:Int, override var count: Int):IStateAndCount
data class DailyHongBaoYu(override var state: Int, override var count: Int):IStateAndCount
data class DailyChengYu(override var state:Int, override var  count: Int):IStateAndCount
data class DailyJiFuZi(override var state:Int, override var count:Int):IStateAndCount

data class TaskInfo(
        val firstTx:FirstTiXian,
        val bushu:DailyBuShu,
        val bindwx:BindWx,
        val share:DailyShare,
        val hbMatch:DailyHongBaoMatch,
        val sport:DailySport,
        val video:DailyVideo,
        val heShui: DailyHeShui,
        val zaoWan: DailyZaoWan,
        val chiFan: DailyChiFan,
        val zhuanPan: DailyZhuanPan,
        val hongBaoYu: DailyHongBaoYu,
        val chengYu: DailyChengYu,
        val jifuzi:DailyJiFuZi
):IDeepCopy<TaskInfo>{
    private fun processStateAndCount(item:ZhuanDataItem, holer:ViewHolder,stateAndCount:IStateAndCount) =
            with(holer){
                tvTargetValue.text = "(${stateAndCount.count}/${item.target})"
                if(stateAndCount.state > 0){
                    // 已经领取
                    btn.text = "已领取"
                    btn.setBackgroundResource(R.drawable.dd_btn_gray)
                    BtnState.Geted
                }else{
                    // 还要判断是否已经完成
                    if(stateAndCount.count >= item.target){
                        btn.text = "领取"
                        btn.setBackgroundResource(R.drawable.btn_red)
                        BtnState.Getable
                    }else{
                        btn.text = "去完成"
                        btn.setBackgroundResource(R.drawable.btn_blue)
                        BtnState.UnCompleted
                    }
                }
            }

    fun processUI(type:Type,holder:ViewHolder,item:ZhuanDataItem) = when(type){
        Type.Shoucitixian -> processStateAndCount(item,holder,firstTx)
        Type.BuShu -> processStateAndCount(item,holder,bushu)
        Type.BangdingWX -> processStateAndCount(item,holder,bindwx)
        Type.Share -> processStateAndCount(item,holder,share)
        Type.TianTianHongBao -> processStateAndCount(item,holder,hbMatch)
        Type.TianTianYunDong -> processStateAndCount(item,holder,sport)

        // has target
        Type.ViewVideo -> processStateAndCount(item,holder,video)
        Type.HeShui -> processStateAndCount(item,holder,heShui)
        Type.ZhuanPan -> processStateAndCount(item,holder,zhuanPan)
        Type.ChengYu -> processStateAndCount(item,holder,chengYu)
        Type.HongBaoYu -> processStateAndCount(item,holder,hongBaoYu)
        Type.JiFuZi -> processStateAndCount(item,holder,jifuzi)
        Type.ZaoWanDaKa -> processStateAndCount(item,holder,zaoWan)
        Type.ChiFan -> processStateAndCount(item,holder,chiFan)
    }

    fun setStateToGeted(type:Type) = when(type){
        Type.Shoucitixian -> firstTx.setToGeted()
        Type.BuShu -> bushu.setToGeted()
        Type.BangdingWX -> bindwx.setToGeted()
        Type.Share -> share.setToGeted()
        Type.TianTianHongBao -> hbMatch.setToGeted()
        Type.TianTianYunDong -> sport.setToGeted()

        Type.ViewVideo ->video.setToGeted()
        Type.HeShui -> heShui.setToGeted()
        Type.ZhuanPan -> zhuanPan.setToGeted()
        Type.ChengYu -> chengYu.setToGeted()
        Type.HongBaoYu -> hongBaoYu.setToGeted()
        Type.JiFuZi -> jifuzi.setToGeted()
        Type.ZaoWanDaKa -> zaoWan.setToGeted()
        Type.ChiFan -> chiFan.setToGeted()
    }

    override fun deepCopy():TaskInfo{
        return TaskInfo(
                firstTx = firstTx.copy(),
                bushu = bushu.copy(),
                bindwx = bindwx.copy(),
                share =  share.copy(),
                hbMatch = hbMatch.copy(),
                sport = sport.copy(),
                video = video.copy(),
                heShui = heShui.copy(),
                zaoWan = zaoWan.copy(),
                chiFan = chiFan.copy(),
                zhuanPan = zhuanPan.copy(),
                hongBaoYu = hongBaoYu.copy(),
                chengYu = chengYu.copy(),
                jifuzi = jifuzi.copy()
        )
    }
}